package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.service.AppletOrOfficialAccountService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dao.UserCommonDao;
import com.huilian.iotbox.data.dto.AppletOrOfficialAccountDto;
import com.huilian.iotbox.data.po.AppletOrOfficialAccount;
import com.huilian.iotbox.data.po.PushConfigTemplate;
import com.huilian.iotbox.data.po.User;
import com.huilian.iotbox.data.service.AppletOrOfficialAccountCommonService;
import com.huilian.iotbox.data.service.PushConfigTemplateCommonService;
import com.huilian.iotbox.data.vo.AppletOrOfficialAccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author LinYinDong
 * @date 2024/6/29 上午11:18
 */
@Service
public class AppletOrOfficialAccountServiceImpl implements AppletOrOfficialAccountService {

    @Autowired
    private UserCommonDao userCommonDao;
    @Autowired
    private PushConfigTemplateCommonService pushConfigTemplateCommonService;
    @Autowired
    private AppletOrOfficialAccountCommonService appletOrOfficialAccountCommonService;

    @Override
    public ServerResponse subscribeAuthorizeByAppId(AppletOrOfficialAccountDto accountDto) {
        if (StringUtils.isEmpty(accountDto.getAppId())) {
            return ServerResponse.createBySuccess("缺少AppID参数");
        }
        if (StringUtils.isEmpty(accountDto.getType())) {
            return ServerResponse.createBySuccess("缺少模板参数ID");
        }
        final AppletOrOfficialAccount appletOrOfficialAccount = appletOrOfficialAccountCommonService.findOneByWeChatAppId(accountDto.getAppId());
        if (StringUtils.isEmpty(appletOrOfficialAccount)) {
            return ServerResponse.createBySuccess( "小程序不存在");
        }
        final PushConfigTemplate configTemplate = pushConfigTemplateCommonService.findOneTemplateId(appletOrOfficialAccount.getId(),accountDto.getType());
        return ServerResponse.createBySuccess(configTemplate);
    }

    @Override
    public ServerResponse findAppletOrOfficialAccountRemarkName(AppletOrOfficialAccountDto appletOrOfficialAccount) {
        final User user = userCommonDao.findUserById(appletOrOfficialAccount.getUserId());
        if (user == null) {
            return ServerResponse.createByErrorMessage("获取信息失败");
        }
        AppletOrOfficialAccountVo appletOrOfficialAccountVo = new AppletOrOfficialAccountVo();
        //公众号
        if (!StringUtils.isEmpty(user.getOfficialAccountId()) || !StringUtils.isEmpty(user.getOfficialAccountOpenId())) {
            if (StringUtils.isEmpty(user.getOfficialAccountId()) && !StringUtils.isEmpty(user.getOfficialAccountOpenId())) {
                //默认为 3
                user.setOfficialAccountId(3);
            }
            final AppletOrOfficialAccount oneById = appletOrOfficialAccountCommonService.findOneById(user.getOfficialAccountId());
            appletOrOfficialAccountVo.setOfficialRemarkName(oneById.getRemarkName());
        }


        //小程序
        if (!StringUtils.isEmpty(user.getAppletAccountId()) || !StringUtils.isEmpty(user.getAppletAccountOpenId())) {
            if (StringUtils.isEmpty(user.getAppletAccountId()) && !StringUtils.isEmpty(user.getAppletAccountOpenId())) {
                //默认为 3
                user.setAppletAccountId(3);
            }
            final AppletOrOfficialAccount oneById = appletOrOfficialAccountCommonService.findOneById(user.getAppletAccountId());
            appletOrOfficialAccountVo.setAppletRemarkName(oneById.getRemarkName());
        }
        return ServerResponse.createBySuccess(appletOrOfficialAccountVo);
    }
}
